<?php

namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
use App\Models\Order;
use App\Models\OrderYlytaking;
use App\Tools\MoneyChange;
use App\Tools\Jpush;
class CallbackController extends Controller
{
    //易联云回调
    public function ylycallback(Request $request){

        //参数处理
        if(!$request->order_id || !isset($request->state) || !$request->cmd)
        {
            return 'fail1';
        }
        //推送模式
        if($request->cmd != 'oauth_getOrder'){
            return 'fail2';
        }
        //获取接单拒单ID
        $taking = OrderYlytaking::where('yly_id',$request->order_id)->first();
        if(!$taking){
            return 'fail3';
        }
        $order=Order::where('order_status',2)->where('order_type',0)->find($taking->order_id);
        if(!$order){
            return 'fail4';
        }
        if($request->state==1){
            $order->order_status = 3;
            $order->save();
            $taking->status=1;
            $taking->save();
            //推送极光空消息
            $jpush = new Jpush();
            $jpush->stopcalling($order->id);

        }else if($request->state==0){
            $money = new MoneyChange();
            $money->add_money($order->user_id,$order->order_amount,1,'（商家拒单）订单编号：'.$order->order_number,0);
            $order->order_status = -3;
            $order->save();
            $taking->status=1;
            $taking->save();
            //推送极光空消息
            $jpush = new Jpush();
            $jpush->stopcalling($order->id);
        }else{
            return 'fail5';
        }
    }



    public function ylygetCache(){
        dd( Cache::get('lyjlyj'));
    }
}
